#!/usr/bin/bash

ENV="prod"

DOCKER_IMAGE_NAME="ai_demo_facade"

CURR_DIR=$(cd "$(dirname $0)"; pwd)

echo ">>> run cmd: cd ${CURR_DIR}"
cd ${CURR_DIR}
echo ""

echo ">>> run cmd: npm run build"
npm run build
echo ""

echo ">>> run cmd: docker rmi -f ${DOCKER_IMAGE_NAME}"
docker rmi -f ${DOCKER_IMAGE_NAME}
echo ""

echo ">>> run cmd: docker build --build-arg ENVIRONMENT=$ENV -t ${DOCKER_IMAGE_NAME} ."
docker build \
    --build-arg ENVIRONMENT=$ENV \
    -t "${DOCKER_IMAGE_NAME}" .
echo ""

echo ">>> run cmd: docker save -o ${CURR_DIR}/${DOCKER_IMAGE_NAME}.${ENV}.tar ${DOCKER_IMAGE_NAME}"
docker save -o ${CURR_DIR}/${DOCKER_IMAGE_NAME}.${ENV}.tar ${DOCKER_IMAGE_NAME}

echo ">>> upload image to project.happy-dolphin.fun ..."
scp ${DOCKER_IMAGE_NAME}.${ENV}.tar project:/tmp/${DOCKER_IMAGE_NAME}.tar
echo ""

echo ">>> redeploying ..."
ssh project 'bash -s' < $CURR_DIR/re-deployment.sh